var baseurl=getApiRoot();
var vm = new Vue({
    el:'#rrapp',
    data:{
    	showList: true,
        title: null,
        tmodel:{        		
        	id:null,
        	t0ordercount:0,
        	t0totalamount:0,
        	t1ordercount:0,
        	t1totalamount:0,
        	personCount:0
        }
    },
    mounted : function() {
		this.$nextTick(function() {
			this.initDaylyReport();
		});
	},
	methods: {
		initDaylyReport:function(){
			$.ajax({
				url : baseurl+'/api/todayReport/getDailyReportByCmp',
				type : 'post',
				dataType:'jsonp',
				cache: 'false',
				async : true,
				data :  setFullParameter({}),
				traditional : true,
				success : function (r){	
				    if(r.code==200){
	  					vm.tmodel=r.data;
				    }else{}
				}		
			});
			$.ajax({
				url : baseurl+'/api/todayReport/getHourlyReportDataByCmp',
				type : 'post',
				dataType:'jsonp',
				cache: 'false',
				async : true,
				data :  setFullParameter({arriveType: 0}),
				traditional : true,
				success : function (r){	
				    if(r.code==200){
			            var list = r.data;
			            if (list.length > 0) {
			                var d = prepareData(list);
			                bar(d, "t0");
			            } else {}
				    }else{}
				}		
			});
			$.ajax({
				url : baseurl+'/api/todayReport/getHourlyReportDataByCmp',
				type : 'post',
				dataType:'jsonp',
				cache: 'false',
				async : true,
				data :  setFullParameter({arriveType: 1}),
				traditional : true,
				success : function (r){	
				    if(r.code==200){
				    	var list = r.data;
		                if (list.length > 0) {
		                    var d = prepareData(list);
		                    bar(d, "t1");
		                } else {}
				    }else{}
				}		
			});
			$.ajax({
				url : baseurl+'/api/sysNotice/querySysNoticeForDashboard',
				type : 'post',
				dataType:'jsonp',
				cache: 'false',
				async : true,
				data :  setFullParameter({}),
				traditional : true,
				success : function (r){	
				    if(r.rows.length>0){
				    	var list = r.rows;
		                if (list.length > 0) {
		                	 prepareNotice(list);
		                } else {}
				    }else{}
				}		
			});
  		}  		 
	}
});

function prepareNotice(a) {
    var copy = JSON.parse(JSON.stringify(a));
    var html = "";
    for (var i = 0; i < copy.length; i++) {
        html += '<div class="list-group">';
        html += '<div class="list-group-item">';
        html += '<h4 class="list-group-item-heading">';
        html += copy[i].title;
        html += '</h4>';
        html += '<p class="list-group-item-text">';
        html += '发布时间：' + copy[i].publishTime;
        html += '</p>';
        html += '<p class="list-group-item-text">';
        html += copy[i].text;
        html += '</p>';
        html += '</div>';
        html += '</div>';
    }
    $("#divNotice").html(html);
}

function prepareData(a) {
    if ((a.length <= 0) || (a.length === 24)) return a;
    var copy = (JSON.parse(JSON.stringify(a)));
    var re = [];
    var ai = 0;
    for (var i = 0; i < 24; i++) {
        if ((ai < copy.length) && (parseInt(copy[ai].hourly) === i)) {
            re.push(copy[ai]); ai++;
        }
        else {
            re.push({ hourly: i.toString(), totalamount: "0", ordercount: "0" })
        }
    }
    return re;
}

function bar(data, chartClass) {
    var bodyWidth = $('.divSvg').width();
    var mleft = 50;
    if (chartClass === "t0") {
        mleft = 70;
    }
    var margin = { top: 10, right: 10, bottom: 30, left: mleft };
    var width = bodyWidth - margin.left - margin.right;
    var height = 250 - margin.top - margin.bottom;

    var x = d3.scale.ordinal()
        .domain(data.map(function (d) { return d.hourly; }))
        .rangeRoundBands([0, width], .1);

    var y = d3.scale.linear()
        .range([height, 0]);

    y.domain([0, d3.max(data, function (d) { return parseInt(d.totalamount); })]);

    $("." + chartClass).empty();
    var chart = d3.select("." + chartClass)
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    var xAxis = d3.svg.axis()
        .scale(x)
        .orient("bottom");

    var yAxis = d3.svg.axis()
        .ticks(5)
        .scale(y)
        .orient("left");

    chart.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    chart.append("g")
        .attr("class", "y axis")
        .call(yAxis);

    chart.selectAll(".bar")
        .data(data)
        .enter().append("rect")
        .attr("class", "bar")
        .attr("x", function (d) { return x(d.hourly); })
        .attr("y", function (d) { return y(d.totalamount); })
        .attr("height", function (d) { return height - y(d.totalamount); })
        .attr("width", x.rangeBand());
}
